import { observable, action, isObservable, computed } from 'mobx';
import NodeWithList from 'stores/models/core/nodeWithList/base';
import Enclosure from 'stores/models/other/enclosure';

/**
 * Element
 * Image, Text...
 */
export default class Element extends NodeWithList {
  originType = 'Element';
  isSupportMotion = true;

  // test enclosure
  @observable enclosure = new Enclosure();

  @observable enterTransition = {
    key: '',
    delay: 0,
    duration: 1
  };
  @observable strongTransition = {
    key: '',              //
    delay: 0,              // 延迟时间
    duration: 1,           // 持续时间
    times: 1,              // 播放次数
    interval: 0            // 停顿时间
  };
  @observable leaveTransition = {
    key: '',
    delay: 0,
    duration: 1
  };

  // 选中自己
  @action selectSelf(multi = false) {
    if (this.parent) {
      this.parent.select(this, multi);
    }
  }
  // 取消选中自己
  @action unSelectSelf() {
    if (this.parent && this.parent.current && this.parent.current === this) {
      this.parent.current.selected = false;
    } else {
      this.selected = false;
    }
  }

  @action removeSelf() {
    this.parent.remove(this);
    this.parent.unSelect();
  }

  @action addOneLayerSelf() {
    if (this.parent) {
      let parentList = this.parent.list;
      let currentPosIndex = parentList.indexOf(this);
      let nextPosIndex = currentPosIndex < parentList.length - 1 ? currentPosIndex + 1 : parentList.length - 1;
      if (nextPosIndex !== currentPosIndex) {
        let mid = parentList[nextPosIndex]; // 保存下一个位置的元素
        parentList[nextPosIndex] = this;
        parentList[currentPosIndex] = mid;
      }
    }
  }

  @action decOneLayerSelf() {
    // console.log('this.parent====', this.parent);
    if (this.parent) {
      let parentList = this.parent.list;
      let currentPosIndex = parentList.indexOf(this);
      let prePosIndex = currentPosIndex === 0 ? 0 : currentPosIndex - 1;
      if (prePosIndex !== currentPosIndex) {
        let mid = parentList[prePosIndex]; // 保存下一个位置的元素
        parentList[prePosIndex] = this;
        parentList[currentPosIndex] = mid;
      }
    }
  }

  @action moveToToplayerSelf() {
    if (this.parent) {
      let parentList = this.parent.list;
      let currentPosIndex = parentList.indexOf(this);
      parentList.splice(currentPosIndex, 1);
      this.parent.list.splice(this.parent.list.length, 0, this);
    }
  }

  @action moveToBottomlayerSelf() {
    if (this.parent) {
      let parentList = this.parent.list;
      let currentPosIndex = parentList.indexOf(this);
      parentList.splice(currentPosIndex, 1);
      this.parent.list.splice(0, 0, this);
    }
  }
}
